package com.hpe.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created by tiansha on 2015/11/12.
 */
@Configuration
public class DefaultMessageListener {

    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    public DefaultMessageListenerContainer messageListener() {
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory(this.connectionFactory);
        container.setDestinationName("testQueue");
        container.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                try {
                    System.out.println(message.getBody(Object.class));
                }
                catch (JMSException ex) {
                    ex.printStackTrace();
                }
            }
        });
        return container;
    }

}
